function isArray(testObject) {   
    return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}

function openEval(stmt, page, response) {
    return this.eval(stmt);
}

exports.onRequest = (function() {
    var walkTemplate = function(t, write, initializing) {
        var //evaluator = new EvalScope(),
            inlineExp = /<([%\?])=(.*)\1>/,
            blockStartExp = initializing?/<([%\?])(@)?/:/<([%\?])(=)?/,
            blockEndExp,
            buffer = "",
            stmt,
            page = {
                responseCode : 200,
                headers : { "content-type" : "text/html" }
            },
            response = {
                write : function(data) {
                    buffer = buffer + data;
                }
            };
        var be1 = blockStartExp.exec(t),
            be2,
            bi1,
            bi2;
        while (be1) {
            blockEndExp = new RegExp((be1[1]==="?"?"\\?":be1[1]) + ">");
            be2 = blockEndExp.exec(t);
            bi1 = t.indexOf(be1[0]);
            if (initializing && bi1>0) { 
                response.write("<?@ ... ?> blocks must be at the top of the page");
                return page;
            }
            bi2 = t.indexOf(be2[0], bi1);
            stmt = t.substr(bi1+(be1[2]?3:2), bi2-bi1-(be1[2]?3:2));
            buffer = "";
            if (initializing || !be1[2]) {
                eval(stmt); //switch to process.compile or Script.runInNewContext
            } else {
                response.write(eval(stmt));
            }
            t = t.substr(0, Math.max(bi1, 0)) 
                + buffer
                + t.substr(bi2+2);
            page.content = t;
            be1 = blockStartExp.exec(t);
        }
        if (!initializing) {
            write(t||"");
        }
        return page;
    };
    
    return function (request, response, hostConfig) {
        console.log("starting request");
        var url = require('url').parse(request.url, true);
        var pathname = url.pathname.replace(/\.\./g, "").replace(/^\//, "");
        if (!pathname) {
            pathname = hostConfig.defaultPage;
        }
        pathname = (hostConfig.baseDirectory) + pathname;
        console.log(pathname);
        var fs = require("fs");
        fs.readFile(pathname, "utf-8", function (err, data) {
            if (err) {
                console.log("ERR");
            }
            template = data; 
            var page = { },
                headerWritten = false, 
                writeHeader = function(responseCode, headers) {
                    if (headerWritten) {
                        return;
                    }
                    headerWritten = true;
                    response.writeHead(
                        page.statusCode||200, 
                        page.headers||{"content-type":"text/html"});
                },
                write = function(data, encoding, statusCode) {
                    console.log(data);
                    writeHeader(statusCode||template?template.statusCode:0, template?template.headers:null);
                    response.write(data, encoding||"utf-8");
                };
            
            if (err) {
                console.log("ERR");
                response.write(JSON.stringify(err), "ascii", 404);
            } else {
                try {
                    page = walkTemplate(template, null, true); //initialize
                    walkTemplate(page.content, write);
                } catch(ex) {
                    page.responseCode = 501;
                    write(ex.toString());
                }
            }
            response.end();
        });
    };

})();